package cn.pugle.json.jackson;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.junit.Test;

import java.io.IOException;

/**
 * 拍平.
 * 类似的有 @JsonAnyGetter
 * @author tzp
 * @since 2018/9/20
 */
@Data
public class TestUnwrap {
    String testName;
    @JsonUnwrapped
    Dog dog;

    @Test
    public void test() throws IOException {
        Dog dog = new Dog();
        dog.setName("doggie");
        dog.setAge(1);

        TestUnwrap testUnwrap = new TestUnwrap();
        testUnwrap.setTestName("fff");
        testUnwrap.setDog(dog);

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(testUnwrap));

        String json = "{\"testName\":\"eee\",\"age\":1,\"the_name\":\"doggie\"}";
        TestUnwrap testUnwrap1 = objectMapper.readValue(json, TestUnwrap.class);
        System.out.println(testUnwrap1);
    }
}
